package io.e2x.testground.fruitstore;

import io.e2x.testground.fruitstore.common.CouponType;
import io.e2x.testground.fruitstore.dal.entities.Sku;
import io.e2x.testground.fruitstore.dal.entities.Store;
import io.e2x.testground.fruitstore.dal.entities.StoreSpu;
import io.e2x.testground.fruitstore.dal.entities.User;
import io.e2x.testground.fruitstore.data.BillDTO;
import io.e2x.testground.fruitstore.data.Coupon;
import io.e2x.testground.fruitstore.service.ProductService;
import io.e2x.testground.fruitstore.service.StoreService;
import io.e2x.testground.fruitstore.service.TradeService;
import io.e2x.testground.fruitstore.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;

@SpringBootApplication
public class FruitstoreApplication {

	@Autowired
	TradeService tradeService;
	@Autowired
	StoreService storeService;
	@Autowired
	UserService userService;
	@Autowired
	ProductService productService;

	public static void main(String[] args) {
		SpringApplication.run(FruitstoreApplication.class, args)
				.getBean(FruitstoreApplication.class)
				.test();
	}
	/**
	 * 测试内容
	 */
	public void test() {
		// 新建超市
		Store store = new Store("超市1", "超市地址", "88888888", "虚拟超市");
		storeService.addStore(store).then().block();
		store = storeService.getStores().filter(store1 -> store1.getName().equals("超市1")).blockFirst();
		// 添加苹果和草莓
		Sku apple = new Sku("苹果", store.getId(), BigDecimal.valueOf(8), 999, 999);
		Sku strawberry = new Sku("草莓", store.getId(), BigDecimal.valueOf(13), 999, 999);
		StoreSpu appleSpu = addStoreSpu(store,  apple, false, BigDecimal.valueOf(1), 0);
		StoreSpu strawberrySpu = addStoreSpu(store, strawberry, false, BigDecimal.valueOf(1), 0);

		// 新建用户
		User userA = addUser("userA", "19900000000");

		ArrayList<StoreSpu> storeSpus = new ArrayList<>();
		// 设置购买数量
		appleSpu.setCount(2);
		storeSpus.add(appleSpu);
		strawberrySpu.setCount(1);
		storeSpus.add(strawberrySpu);
		// A买家交易
		tradeService.trade(store, userA, storeSpus, null).block();
		Sku mongo = new Sku("芒果", store.getId(), BigDecimal.valueOf(20), 999, 999);
		StoreSpu mongoSpu = addStoreSpu(store, mongo, false, BigDecimal.valueOf(1), 0);

		// 添加B用户
		User userB = addUser("userB", "19900000001");
		// 设置B用户购买数量
		appleSpu.setCount(2);
		strawberrySpu.setCount(2);
		mongoSpu.setCount(8);
		storeSpus.clear();
		storeSpus.add(strawberrySpu);
		storeSpus.add(appleSpu);
		storeSpus.add(mongoSpu);
		// B买家交易
		tradeService.trade(store, userB, storeSpus, null).block();
		// 设置草莓折扣
		strawberrySpu.setDiscount(BigDecimal.valueOf(0.8));
		productService.updateSpu(strawberrySpu, strawberry).then().block();
		// 添加C用户
		User userC = (new User("userC", "19900000002"));
		userC = userService.addUser(userC).then(userService.getUserByUserName("userC")).block();
		storeSpus.clear();
		// 设置C用户购买数量
		strawberrySpu.setCount(2);
		appleSpu.setCount(2);
		mongoSpu.setCount(8);
		storeSpus.add(strawberrySpu);
		storeSpus.add(appleSpu);
		storeSpus.add(mongoSpu);
		// C买家交易
		tradeService.trade(store, userC, storeSpus, null).block();
		// 添加D用户
		User userD = (new User( "userD", "19900000003"));
		userD = userService.addUser(userD).then(userService.getUserByUserName("userD")).block();
		storeSpus.clear();
		// 设置D用户购买数量
		strawberrySpu.setCount(2);
		appleSpu.setCount(2);
		mongoSpu.setCount(8);
		storeSpus.add(strawberrySpu);
		storeSpus.add(appleSpu);
		storeSpus.add(mongoSpu);
		// D买家交易
		tradeService.trade(store, userD, storeSpus, new Coupon("满100减10", CouponType.REDUCTION, BigDecimal.valueOf(10), BigDecimal.valueOf(100))).block();
	}

	/**
	 * 添加用户
	 * @param name
	 * @param phone
	 * @return
	 */
	private User addUser(String name, String phone) {
		User newUser = new User(name, phone);
		userService.addUser(newUser).block();
		return userService.getUserByUserName(name).block();
	}
	/**
	 * 添加商品
	 * @param store
	 * @param sku
	 * @param isDiscount
	 * @param discount
	 * @param count
	 * @return
	 */
	private StoreSpu addStoreSpu(Store store, Sku sku, boolean isDiscount, BigDecimal discount, int count) {
		StoreSpu storeSpu = new StoreSpu(store.getId(), "no"+ System.currentTimeMillis(), isDiscount, discount, count);
		productService.addSpu(storeSpu, sku).block();
		return productService.findBySpuName(sku.getName()).block();
	}

}
